﻿/**
 * Map.cs - Map of tiles, to be used by a level
 * 
 * Part of BoulderDAM - A BoulderDash clone using C# and SDL
 * Nacho Cabanes & students of DAM IES San Vicente, 2014
 * 
 * Revisions
 * #     Date         Creator, changes
 * ----  -----------  -------------------------------------------------------
 * 0.01  02-apr-2014  Nacho Cabanes: Basic skeleton
 * 0.02  03-apr-2014  Miluz Garcia, Alejandra De Freitas, Omar Marin
 * 0.03  04-apr-2014  Nacho Cabanes: Map of tiles can be redefined in subclasses,
 *                        Empty skeleton for MoveAndGetPointsFrom and CanFallTo
 * 0.04  07-apr-2014  Tomas Martinez, Daniel Segura, Jose Estrada, we have redisegn collision methods
 */

namespace Boulder
{
    class Map
    {
        const int ITEMS_HORIZ = 20;
        const int ITEMS_VERT = 12;
        const int ITEM_WIDTH = 48;
        const int ITEM_HEIGHT = 48;

        protected string[] map;   // To be redefined in subclasses

        // Temporary: items to be tested
        Enemy enemy; 
        Item item;
        Stone stone;
        Sprite wall;
        Sprite sand;
        Sprite door;

        public Map()
        {
             map = new[]  {  // To be redefined in subclasses
              "                    ", 
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    ",
              "                    " };

            wall = new Sprite("data/wall.png");
            sand = new Sprite("data/sand.png");
            enemy = new Enemy();
            item = new Item();
            stone = new Stone();
            door = new Sprite("data/doorClosed.png");
        }

        public virtual void DrawOnHiddenScreen()
        {
            //Player P
            //Wall W
            //Sand S
            //Stone R
            //Item I
            //Door D
            //Enemy E

            // Background map
            // TO DO
            for (int x = 0; x < ITEMS_HORIZ; x++)
            {
                for (int y = 0; y < ITEMS_VERT; y++)
                {
                    switch (map[y][x])
                    {
                        case 'W':
                            wall.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            wall.DrawOnHiddenScreen();
                            break;
                        case 'S':
                            sand.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            sand.DrawOnHiddenScreen();
                            break;
                        case 'R':
                            stone.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            stone.DrawOnHiddenScreen();
                            break;
                        case 'I':
                            item.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            item.DrawOnHiddenScreen();
                            break;
                        case 'D':
                            door.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            door.DrawOnHiddenScreen();
                            break;
                        case 'E':
                            enemy.MoveTo(x * ITEM_WIDTH, y * ITEM_HEIGHT);
                            enemy.DrawOnHiddenScreen();
                            break;
                    }
                }
            }

            // Temporary: items to be tested
            wall.MoveTo(100, 100);
            wall.DrawOnHiddenScreen();
            sand.MoveTo(200, 200);
            sand.DrawOnHiddenScreen();
            //enemy.MoveTo(100, 200);
            enemy.DrawOnHiddenScreen();
            item.MoveTo(200, 100);
            item.DrawOnHiddenScreen();
            stone.MoveTo(100, 20);
            stone.DrawOnHiddenScreen();
        }


        // Returns "true" if player can move to a certain coordinates
        public bool CanMoveTo(int xStart, int yStart, int xEnd, int yEnd)
        {
            // If it touches any brick, it cannot move to those coordinates
            // TO DO
            if ((xStart > xEnd - ITEM_WIDTH)
                        && (xStart < xEnd + ITEM_WIDTH)
                        && (yStart > yEnd - ITEM_HEIGHT)
                        && (yStart < yEnd + ITEM_HEIGHT))
                return true;
            return false;
        }


        // Returns "true" if item can fall to a certain coordinates
        public bool CanFallTo(int xStart, int yStart, int xEnd, int yEnd)//maybe parameters not needed
        {

            // If there is no sand beneath, items and stones should fall

            for (int x = 0; x < 19; x++)//x 20
            {
                for (int y = 0; y < 10; y++)//y 12
                {
                    if (map[x][y] == 'R' && map[x][y+1]==' ')
                    {
                        return true;
                    }
                }
            }

            return false;
        }


        // Returns number of points of items in certain coordinates
        // and removes those items from the map
        public int MoveAndGetPointsFrom(int xStart, int yStart, int xEnd, int yEnd)
        {
            // TO DO

            return 0;
        }


        public void AnimateElements()
        {
            item.Animate();
            enemy.Animate();
            stone.Animate();
        }
    }
}
